package com.shenmazong.serverwebpoem.util;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 田不平
 * @project server-web-poem
 * @date 2020/3/29 10:27 AM
 * http://www.shenmazong.com
 */
public class ParamUtils {

    /**
     * TODO 获取字符类型的参数，默认值为""
     * @param request
     * @param name
     * @return
     */
    public static String getParameter(HttpServletRequest request, String name) {
        String temp = request.getParameter(name);

        if (temp == null) {
            return "";
        }
        return temp;
    }

    /**
     * TODO 获取字符类型的参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultValue
     * @return
     */
    public static String getParameter(HttpServletRequest request, String name,
                                      String defaultValue) {
        String temp = request.getParameter(name);
        if (temp == null || "".equals(temp)) {
            return defaultValue;
        }

        return temp;
    }

    /**
     * TODO 获取布尔型的参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultVal
     * @return
     */
    public static boolean getBooleanParameter(HttpServletRequest request, String name,
                                              boolean defaultVal) {
        String temp = request.getParameter(name);
        if ("true".equals(temp) || "on".equals(temp)) {
            return true;
        } else if ("false".equals(temp) || "off".equals(temp)) {
            return false;
        } else {
            return defaultVal;
        }
    }

    /**
     * TODO 获取整型的参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static int getIntParameter(HttpServletRequest request, String name,
                                      int defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            int num = defaultNum;
            try {
                num = Integer.parseInt(temp);
            } catch (Exception ignored) {
            }
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * TODO 可以获取整型数组的参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static int[] getIntParameters(HttpServletRequest request, String name,
                                         int defaultNum) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null) {
            return null;
        }
        if (paramValues.length < 1) {
            return new int[0];
        }
        int[] values = new int[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            try {
                values[i] = Integer.parseInt(paramValues[i]);
            } catch (Exception e) {
                values[i] = defaultNum;
            }
        }
        return values;
    }

    /**
     * TODO 获取双精度浮点型参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static double getDoubleParameter(HttpServletRequest request, String name,
                                            double defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            double num = defaultNum;
            try {
                num = Double.parseDouble(temp);
            } catch (Exception ignored) {
            }
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * TODO 可以获取长整型的参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static long getLongParameter(HttpServletRequest request, String name,
                                        long defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            long num = defaultNum;
            try {
                num = Long.parseLong(temp);
            } catch (Exception ignored) {
            }
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * TODO 可以获取短整型参数，可以设置默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static Short getShortParameter(HttpServletRequest request, String name,
                                          Short defaultNum) {
        String temp = request.getParameter(name);
        if (temp != null && !temp.equals("")) {
            Short num = defaultNum;
            try {
                num = Short.valueOf(temp);
            } catch (Exception ignored) {
            }
            return num;
        } else {
            return defaultNum;
        }
    }

    /**
     * TODO 获取长整型数组参数，可以指定默认值
     * @param request
     * @param name
     * @param defaultNum
     * @return
     */
    public static long[] getLongParameters(HttpServletRequest request, String name,
                                           long defaultNum) {
        String[] paramValues = request.getParameterValues(name);
        if (paramValues == null) {
            return null;
        }
        if (paramValues.length < 1) {
            return new long[0];
        }
        long[] values = new long[paramValues.length];
        for (int i = 0; i < paramValues.length; i++) {
            try {
                values[i] = Long.parseLong(paramValues[i]);
            } catch (Exception e) {
                values[i] = defaultNum;
            }
        }
        return values;
    }
}
